package homework.homework1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 完成一个学生管理系统
 *
 * // 功能有
 *         1   添加一个学生
 *         2   根据 id 号 查询学生的信息
 *         3   修改学生的信息（出了id不能修改 其他内容可以修改）
 *         4   查询所有的学生信息
 *         5   根据id 删除学生
 */
public class StudentSystem {
    private List<Student> students = new ArrayList();
    private int id = 0;

    public void run(){
        while(true){
            Scanner scan = new Scanner(System.in);
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择：\n" +
                    "1. 添加一个学生\n" +
                    "2. 根据 id 号 查询学生的信息\n" +
                    "3. 修改学生的信息（出了id不能修改 其他内容可以修改）\n" +
                    "4. 查询所有的学生信息\n" +
                    "5. 根据id 删除学生\n");

            String s = scan.next();

            if("1".equals(s)){
                Student a = newStudent();
                a.setId(++this.id);
                addStudent(a);
                System.out.println("添加成功");
            }else if("2".equals(s)){
                showById(inputId());
            }else if("3".equals(s)){
                update();
            }else if("4".equals(s)){
                Collections.sort(students, new StudentComparator());
                for (Student student : students) {
                    show(student);
                }
            }else if("5".equals(s)){
                deleteById(inputId());
            }
        }
    }

    private void addStudent(Student student){
        students.add(student);
    }

    private void update(){
        int index = haveStudent();
        if(index == -1){
            System.out.println("为找到该编号学生");
        }else{
            Student a = newStudent();
            a.setId(students.get(index).getId());
            students.remove(index);
            students.add(index, a);
            System.out.println("修改成功");
        }
    }

    private void deleteById(int id){
        students.remove(selectById(id));
    }

    private void showById(int id){

        show(selectById(id));
    }

    private Student selectById(int id){
        Student s = null;
        for (Student student : students) {
            if(student.getId() == id){
                s = student;
                break;
            }
        }
        return s;
    }

    private int selectIndex(int id){
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            if(students.get(i).getId() == id){
                index = i;
                break;
            }
        }
        return index;
    }

    private void show(Student student){
        System.out.println("姓名： " + student.getName());
        System.out.println("id:" + student.getId());
        System.out.println("年龄: " + student.getAge());
        System.out.println("性别： "+student.getGender());
        System.out.println("地址： "+student.getName());
    }

    private int haveStudent(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入该同学的id");
        int id = scan.nextInt();
        int index = selectIndex(id);

        return index;
    }

    private int inputId(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入学生id：");
        int id = scan.nextInt();

        return id;
    }

    private Student newStudent(){
        Scanner scan = new Scanner(System.in);

        System.out.println("请输入姓名：");
        String name = scan.next();

        System.out.println("请输入年龄：");
        int age = scan.nextInt();

        System.out.println("请输入性别：");
        String gen = scan.next();

        System.out.println("请输入地址：");
        String address = scan.next();

        Student newStudent = new Student(name, age, gen, address);

        return newStudent;
    }
}
